<!--
 * @Description:
 * @version:
 * @Author: mikeJang
 * @Date: 2021-06-15 19:43:26
 * @LastEditors: mikeJang
 * @LastEditTime: 2021-08-11 11:18:46
 * @FilePath: \YU_LIN_Frontend\src\components\SRuleAdcdTree.vue
-->
<template>
  <a-tree-select v-model="adcd"
                 :multiple="multiple"
                 :max-tag-count="maxTagCount"
                 :max-tag-placeholder="maxTagPlaceholder"
                 :tree-data="treeData"
                 :disabled="disabled"
                 placeholder="请选择"
                 dropdownClassName="select-tree"
                 tree-checkable
                 @change="handelChange">
  </a-tree-select>
</template>

<script>
export default {
  name: "SAdcdTree",
  props: {
    value: {
      type: [String, Array],
      default: ""
    },
    pid: {
      type: String,
      default: "652800000000000"
    },
    multiple: {
      type: Boolean,
      default: false
    },
    maxTagCount: {
      type: Number
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      treeData: [],
      adcd: this.value
    };
  },
  created () {
    this.getData();
  },
  methods: {
    maxTagPlaceholder () {
      return "...";
    },
    getData () {
        const params = {
          isQueryAll: true,
          adlevel: 2,
          pid: this.pid
        }
        this.$http.userauth.queryAdcdJsonTreeByAdlevel(params).then(res => {
          const parseNode = item => {
            return {
              title: item.title,
              value: item.value,
              key: item.value,
              children: item.children && item.children.map(parseNode),
              isLeaf: item.children && !item.children.length,
              disabled: false
            };
          };

          if (!res) {
            return;
          }
          this.treeData = [
            {
              title: "全部",
              value: "all",
              key: "all",
              isLeaf: true,
              disabled: false
            },
            parseNode(res)
          ];
        });
    },
    handelChange (val) {
      if (val.includes("all")) {
        const disfun = item => {
          if (item.value !== "all") {
            item.disabled = true;
            if (item.children) {
              item.children.map(disfun);
            }
          }
        };
        this.treeData.map(disfun);
        this.adcd = ["all"];
      } else {
        const disfun = item => {
          if (item.value !== "all") {
            item.disabled = false;
            if (item.children) {
              item.children.map(disfun);
            }
          }
        };

        this.treeData.map(disfun);
      }
      // this.$emit("change", val);
      this.$emit("input", val);
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep {
  /* .ant-select-sm.ant-select-selection--multiple {
    min-height: 32px;
  } */
}
</style>
